package com.sc.test.recyclerview2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sc.test.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-12-012 下午 21:24.
 */

public class  LinearRvAdapter extends RecyclerView.Adapter<LinearRvAdapter.Viewholder> {
    private ArrayList<String> mData;

    public LinearRvAdapter(ArrayList<String> mData) {
        this.mData = mData;
    }

    /**
     * 事件回调监听
     */
    private LinearRvAdapter.OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(LinearRvAdapter.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * 添加条目
     */
    public void addNewItem() {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(0, "new item");
        notifyItemInserted(0);
    }

    /**
     * 删除条目
     */
    public void deleteItem() {
        if (mData == null || mData.isEmpty()) {
            return;
        }
        mData.remove(0);
        notifyItemRemoved(0);
    }


    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        //实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.view_rv_item, parent, false);
        //实例化viewholder
        Viewholder holder = new Viewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Viewholder holder, int position) {
        //绑定数据
        holder.tv_item.setText(mData.get(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView, pos);
                }
                //不再触发点击事件
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class Viewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_tv)
        TextView tv_item;

        public Viewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
